/**
 * Copyright (C), 2020, JieYi Software System Co., Ltd.
 * All rights reserved.
 * FileName:       SpringSecurityConfig.java
 *
 * @Description:
 * @author: feiwe
 * Modification History:
 * Date         Author        Version        Discription
 * -------------------------------------------------------
 * 2020/12/23      feiwe                         add
 */


package com.fengwei.config;

import com.fengwei.security.AjaxAuthenticationFailureHandler;
import com.fengwei.security.AjaxAuthenticationSuccessHandler;
import com.fengwei.security.AjaxLogoutSuccessHandler;
import com.fengwei.security.RestAccessDeniedHandler;
import com.fengwei.security.RestAuthenticationEntryPoint;
import com.fengwei.security.SelfAuthenticationProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private SelfAuthenticationProvider provider;

    @Autowired
    private AjaxAuthenticationSuccessHandler authenticationSuccessHandler;

    @Autowired
    private AjaxAuthenticationFailureHandler authenticationFailureHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/login/**").permitAll()

                //.antMatchers("/**").fullyAuthenticated()
//                .and().formLogin().loginProcessingUrl(loginApi)
//                .successHandler(new RestAuthenticationSuccessHandler())
//                .failureHandler(new RestAuthenticationFailureHandler())

                .and().formLogin()
                .usernameParameter("username")
                .passwordParameter("password")
                .loginProcessingUrl("/initSpringSecurity")
                .successHandler(authenticationSuccessHandler)
                .failureHandler(authenticationFailureHandler)
                .permitAll()

                .and().logout().logoutUrl("/login/logout")
                .logoutSuccessHandler(new AjaxLogoutSuccessHandler())
                .deleteCookies("JSESSIONID")
                .invalidateHttpSession(true)
                .permitAll()

                .and().rememberMe().rememberMeParameter("remember").rememberMeCookieName("remember")
                // .and().sessionManagement().maximumSessions(1).maxSessionsPreventsLogin(true)
                //.and()

                .and().exceptionHandling().authenticationEntryPoint(new RestAuthenticationEntryPoint())
                .accessDeniedHandler(new RestAccessDeniedHandler())
        ;
        //可以用iframe
        http.headers().frameOptions().disable();
        //关闭csrf
        http.csrf().disable();
    }

    //安全认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(provider);
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}
